//
//  DLViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/11/1.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "DLViewController.h"
#import "DLLBCollectionViewCell.h"
@interface DLViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

@implementation DLViewController
{
    RWcollectionView *_collectionView;
    int _page;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self drawNavWithTitle:@"代理产品专区" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 4 ;
    layout.minimumLineSpacing = 0;
    _collectionView = [[RWcollectionView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64) collectionViewLayout:layout];
    [_collectionView registerClass:[DLLBCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _collectionView.backgroundColor =[UIColor colorWithHue:0.74 saturation:0.03 brightness:0.95 alpha:1.00];
    _collectionView.delegate= self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView addHeaderWithTarget:self action:@selector(headRefresh)];
    [_collectionView addFooterWithTarget:self action:@selector(footerRefresh)];
}

-(void)headRefresh{
    _page = 1;
    [self requestDataWith:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestDataWith:0];
}

//collectionView 的cell 内容请求
-(void)requestDataWith:(int)first{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:@"10" forKey:@"pageSize"];
    [requestDic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [requestDic setObject:@"1" forKey:@"goodsType"];
    
    [NetWorking NEWjieKouQingQiu:YYG_yygPageList ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        [TCUtils hiddonLoadingWithView:self.view];
        [_collectionView footerEndRefreshing];
        [_collectionView headerEndRefreshing];
        if (!error) {
//            if ([dic[Code] intValue] == 200) {
//                if (first == 1) {
//                    _allArr = [[NSMutableArray alloc]init];
//                    _imageArr = [[NSMutableArray alloc]init];
//                    //                    NSArray * image = dic[Response][@"img"];
//                    //                    [image enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                    //                        [_imageArr addObject:obj[@"imgUrl"]];
//                    //                    }];
//                }
//                NSArray * arr = dic[Response][@"resultList"];
//                // _sdcView.imageURLStringsGroup = _imageArr;
//                [arr enumerateObjectsUsingBlock:^(id  obj, NSUInteger idx, BOOL *  stop) {
//                    
//                    YYG_AllFirstModel *yiYuanAll = [YYG_AllFirstModel objectFromDictionary:obj];
//                    [_allArr addObject:yiYuanAll];
//                    yiYuanAll = nil;
//                    
//                }];
//                [_collectionView reloadData];
//            }
        }
    }];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return  CGSizeMake((KScreenWidth-4)/2, 250);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 16;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
   // _allFirst = _allArr[indexPath.row];
    DLLBCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    [cell.image sd_setImageWithURL:[NSURL URLWithString:_allFirst.coverImage]];
//    cell.title.text = _allFirst.name;
    cell.buy.tag = indexPath.row;
    cell.gwc.tag = indexPath.row;
    [cell.buy addTarget:self action:@selector(buy:) forControlEvents:UIControlEventTouchUpInside];
    [cell.gwc addTarget:self action:@selector(gwc:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
//进入详情页
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    YygDetailsViewController * details = [[YygDetailsViewController alloc]init];
//    details.firstModel = _allArr[indexPath.row];
//    [self.navigationController pushViewController:details animated:YES];
}

-(void)buy:(UIButton *)btn{

}
-(void)gwc:(UIButton *)btn{

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
